//
//  ExaminationPaperScoreCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/3.
//

#import "ExaminationPaperScoreCell.h"

@interface ExaminationPaperScoreCell()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *scoreLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *totalTimeLabel;
@property(nonatomic,strong)UIView *scoreBgView;

@end

@implementation ExaminationPaperScoreCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = KbgColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.top.bottom.mas_equalTo(@0);
    }];
    UITapGestureRecognizer *checkTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkClick)];
    [self.bgView addGestureRecognizer:checkTap];
    
    self.scoreLabel = [[UILabel alloc]init];
    self.scoreLabel.userInteractionEnabled = YES;
    [self.bgView addSubview:self.scoreLabel];
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@16);
        make.height.mas_equalTo(@34);
        make.right.mas_equalTo(@-20);
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textColor = KFontGrayColor;
    self.timeLabel.font = JPUIFontPingFangSCRegular(13);
    self.timeLabel.userInteractionEnabled = YES;
    [self.bgView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.scoreLabel.mas_left);
        make.height.mas_equalTo(@18);
        make.top.mas_equalTo(self_weak_.scoreLabel.mas_bottom).offset(3);
        make.right.mas_equalTo(@-20);
    }];
    
    self.scoreBgView = [[UIView alloc]init];
    self.scoreBgView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:self.scoreBgView];
    [self.scoreBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(@0);
        make.top.mas_equalTo(self_weak_.timeLabel.mas_bottom).offset(18);
        make.height.mas_equalTo(@0);
    }];
    
    
    UIButton *testBtn = [[UIButton alloc]init];
    testBtn.backgroundColor = KThemeColor;
    testBtn.layer.cornerRadius = 21;
    testBtn.layer.masksToBounds = YES;
    [testBtn setTitle:@"重新考试" forState:UIControlStateNormal];
    [testBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    testBtn.titleLabel.font = JPUIFontPingFangSCMedium(16);
    [testBtn addTarget:self action:@selector(testBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:testBtn];
    [testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-20);
        make.left.mas_equalTo(@20);
        make.right.mas_equalTo(@-20);
        make.height.mas_equalTo(@42);
    }];
    
    
    self.totalTimeLabel = [[UILabel alloc]init];
    self.totalTimeLabel.font = JPUIFontPingFangSCRegular(16);
    self.totalTimeLabel.textColor = KFontBlackColor;
    self.totalTimeLabel.textAlignment = NSTextAlignmentCenter;
    self.totalTimeLabel.userInteractionEnabled = YES;
    [self.bgView addSubview:self.totalTimeLabel];
    [self.totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(testBtn.mas_top).offset(-10);
        make.height.mas_equalTo(@22);
        make.left.mas_equalTo(@20);
        make.right.mas_equalTo(@-20);
    }];
    
}

#pragma mark - 创建分数进度UI
-(void)createProgressUIWithArr:(NSArray*)arr{
    [self.scoreBgView removeAllSubviews];
    CGFloat top = 0;
    for(NSInteger i = 0;i < arr.count;i++){
        ExamPaperResultListModel *listModel = arr[i];
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = JPUIFontPingFangSCRegular(13);
        titleLabel.textColor = KFontGrayColor;
        titleLabel.userInteractionEnabled = YES;
        titleLabel.text = listModel.typeName;
        [self.scoreBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@20);
            make.height.mas_equalTo(@18);
            make.right.mas_equalTo(@-20);
            make.top.mas_equalTo(top);
        }];
        top = top+18+30;
        UIProgressView *progressView = [[UIProgressView alloc]init];
        progressView.backgroundColor = KbgColor;
        progressView.layer.cornerRadius = 5/2.0;
        progressView.layer.masksToBounds = YES;
        progressView.progressTintColor = KThemeColor;
        progressView.trackTintColor = KbgColor;
        progressView.userInteractionEnabled = YES;
        if(listModel.actualScore.doubleValue > 0){
            if((listModel.score.doubleValue/listModel.actualScore.doubleValue) > 1){
                progressView.progress = 1;
            }else{
                progressView.progress = listModel.score.doubleValue/listModel.actualScore.doubleValue;
            }
        }else{
            progressView.progress = 0;
        }
        [self.scoreBgView addSubview:progressView];
        [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@20);
            make.right.mas_equalTo(@-87);
            make.height.mas_equalTo(@5);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(9);
        }];
        UILabel *rightScroeLabel = [[UILabel alloc]init];
        rightScroeLabel.textAlignment = NSTextAlignmentRight;
        rightScroeLabel.userInteractionEnabled = YES;
        rightScroeLabel.attributedText = [self changeStr:[NSString stringWithFormat:@"%ld",listModel.score?listModel.score.integerValue:0] andStr2:[NSString stringWithFormat:@"%ld",listModel.actualScore?listModel.actualScore.integerValue:0]];
        [self.scoreBgView addSubview:rightScroeLabel];
        [rightScroeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(@-20);
            make.left.mas_equalTo(progressView.mas_right).offset(5);
            make.height.mas_equalTo(@18);
            make.centerY.mas_equalTo(progressView);
        }];
    }
    
}

-(NSMutableAttributedString*)changeStr:(NSString*)str{
    NSMutableAttributedString *pStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@分",str]];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontBlackColor range:NSMakeRange(0,pStr.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontInterMedium(28) range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontPingFangSCRegular(12) range:NSMakeRange(str.length,pStr.length-str.length)];
    return pStr;
}

-(NSMutableAttributedString*)changeStr:(NSString*)str andStr2:(NSString*)str2{
    NSMutableAttributedString *pStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@/%@分",str,str2]];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontBlackColor range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontGrayColor range:NSMakeRange(str.length,pStr.length-str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontInterMedium(15) range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontInterRegular(12) range:NSMakeRange(str.length,pStr.length-str.length)];
    return pStr;
}

#pragma mark - 重新考试
-(void)testBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(scoreTestTap:)]){
        [self.delegate scoreTestTap:self];
    }
}

#pragma mark - 查看解析
-(void)checkClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkAnalysisTap:)]){
        [self.delegate checkAnalysisTap:self];
    }
}

-(void)setResultModel:(ExamPaperResultModel *)resultModel{
    _resultModel = resultModel;
    self.scoreLabel.attributedText = [self changeStr:[NSString stringWithFormat:@"%ld",resultModel.sumScore?resultModel.sumScore.integerValue:0]];
    self.timeLabel.text = [self convertTimestampToDate:resultModel.time];
    [self createProgressUIWithArr:resultModel.list];
}

-(void)setTime:(NSString *)time{
    _time = time;
    self.totalTimeLabel.text = [NSString stringWithFormat:@"考试时间：%@分钟",time];
}

-(NSString *)convertTimestampToDate:(NSString *)timestampString {
    // 将时间戳字符串转换为NSTimeInterval
    NSTimeInterval timestamp = [timestampString doubleValue] / 1000;
    
    // 创建一个NSDate对象
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    // 创建一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    // 将NSDate对象转换为字符串
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    return dateString;
}

@end
